package com.coai.message.repository

import com.coai.message.model.ConversationParticipant
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository

@Repository
interface ConversationParticipantRepository : JpaRepository<ConversationParticipant, String> {
    
    fun findByConversationId(conversationId: String): List<ConversationParticipant>
    
    fun findByConversationIdAndUserId(conversationId: String, userId: String): ConversationParticipant?
    
    fun existsByConversationIdAndUserId(conversationId: String, userId: String): Boolean
    
    @Modifying
    @Query("DELETE FROM ConversationParticipant cp WHERE cp.conversationId = :conversationId AND cp.userId = :userId")
    fun deleteByConversationIdAndUserId(conversationId: String, userId: String)
    
    @Query("""
        SELECT cp.userId FROM ConversationParticipant cp
        WHERE cp.conversationId = :conversationId AND cp.userId != :excludeUserId
    """)
    fun findOtherParticipantIds(conversationId: String, excludeUserId: String): List<String>
}
